// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import DataManager from "../manager/DataManager";
import PoolManager from "../manager/PoolManager";
import ResourceManager from "../manager/ResourceManager";
import SdkManager from "../manager/SdkManager";
import ToastManager from "../manager/ToastManager";
import { privacyChannel } from "../Script/privacyChannel";
import bh_toast from "../Script/bh_toast";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PowerLayer extends BaseLayer {

    panel: cc.Node = null
    btnClose: cc.Node = null
    btnUpdate: cc.Node = null
    scrollContent: cc.Node = null
    fill: cc.Node = null
    number: cc.Node = null
    videoTip: cc.Node = null
    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnClose = cc.find('style/panel/buttons/btn_next', this.node)
        this.btnUpdate = cc.find('style/panel/buttons/btn_upgrade', this.node)
        this.fill = cc.find("style/panel/progress/fill", this.node)
        this.number = cc.find("style/panel/buttons/btn_upgrade/number", this.node)
        this.btnClose.on('click', this.onClickClose, this)
        this.btnUpdate.on('click', this.onClickUpGrade, this)
        this.videoTip = cc.find("style/panel/videotip", this.node)
        cc.find("style/panel/videotip/box", this.node).on("click", this.onClickBox, this)

        this.updateProgress()
    }
    onClickClose() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.POWER, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
    }
    onClickUpGrade() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        privacyChannel.getChannel().showVideo(() => {
            cc.sys.localStorage.setItem("powerLevel", parseInt(cc.sys.localStorage.getItem("powerLevel") || '0') + 1)
            cc.resources.load("privacy/bh_toast", cc.Prefab, (error, prefab: cc.Prefab) => {
                if (error) {
                    console.error(error);
                    return;
                }
                let node = cc.instantiate(prefab);
                if (parseInt(cc.sys.localStorage.getItem("powerLevel") || '0') >= 3) {
                    node.getComponent(bh_toast).show("获得：不消耗体力游玩24小时",
                        cc.director.getScene().getChildByName('Canvas').getChildByName("UI"))
                    cc.sys.localStorage.setItem("powerTime", Date.now())

                } else {
                    DataManager.instance.power += (parseInt(cc.sys.localStorage.getItem("powerLevel") || '0') * 10)
                    DataManager.instance.save()
                    node.getComponent(bh_toast).show("获得：体力X" + (parseInt(cc.sys.localStorage.getItem("powerLevel") || '0') * 10),
                        cc.director.getScene().getChildByName('Canvas').getChildByName("UI"))
                }
            })
            this.updateProgress()
        })

    }
    onClickBox() {
        console.error("点击按钮")
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        this.videoTip.getChildByName("mark").active = !this.videoTip.getChildByName("mark").active
        if (this.videoTip.getChildByName("mark").active) {
            this.btnClose.active = false
        } else {
            this.btnClose.active = true
        }
    }
    onDestroy() {
        this.btnClose.off('click', this.onClickClose, this)
        this.btnUpdate.off('click', this.onClickUpGrade, this)
    }
    show(): void {
        super.show()
        //24 * 60 * 60 * 1000
        if (Date.now() <= (parseInt(cc.sys.localStorage.getItem("powerTime")) + 24 * 60 * 60 * 1000)) {
            console.log('目标时间是 24 小时内的时间');

        } else {
            console.log('目标时间超过 24 小时');
            cc.sys.localStorage.setItem("powerLevel", 0)
            this.updateProgress()
        }
        if (privacyChannel.getChannel().getCtrl("p1")) {
            this.videoTip.getChildByName("mark").active = true
            this.btnClose.active = false
        } else {
            this.videoTip.getChildByName("mark").active = false
            this.btnClose.active = true
        }
        switch (privacyChannel.channel) {
            case "ks":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                }
                break
        }
    }
    onEnable() {

    }

    onDisable() {

    }
    updateProgress() {
        this.number.getComponent(cc.Label).string = (parseInt(cc.sys.localStorage.getItem("powerLevel") || '0') + 1) + ""
        this.fill.getComponent(cc.Sprite).fillRange = 0.33 * parseInt(cc.sys.localStorage.getItem("powerLevel") || '0')
    }




}
